# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd

def prepare_data(time_series, situation):
    # 总周数
    week_num = abs(time_series['接警日期'].iloc[-1] - time_series['接警日期'].iloc[0]).days//7
    # 找出所有的事故类别
    accident_sites = list(np.unique(time_series['事件所在的区域']))
    sites_num = len(accident_sites)
    # 
    data = np.zeros((sites_num, 2))
    for i in range(sites_num):
        accident_site = accident_sites[i]
        # 某地点的情况
        situation_site = situation.loc[situation['编号']==accident_site]
        # 面积
        site_area = situation_site['面积（km2）'].values
        site_area = np.float(site_area)
        # 某地点发生的事故次数（16-20年）
        accident_times = time_series\
                        .loc[time_series['事件所在的区域']\
                        ==accident_site].iloc[:, 0].count()
        
        # 事件密度
        data[i, 0] = accident_times/(week_num*site_area)
        # 人口密度
        site_population = situation_site['人口（万人）'].values
        data[i, 1] =site_population/site_area
        
    data = pd.DataFrame(data=data, columns=['事件密度', '人口密度'])
    return data    

if __name__ == '__main__':
    path = r'../附件/附件2：某地消防救援出警数据.xlsx'
    time_series = pd.read_excel(path)
    path = r'../附件/附件1：各区域的人口、面积.xlsx'
    situation = pd.read_excel(path)
    data = prepare_data(time_series, situation)
    
    # 保存数据
    data.to_excel(r'../附件/事件密度-人口密度.xlsx')
